
import pandas as pd 
import joblib
class AIrecommender :
    print("loading models")
    ad_sim = joblib.load("model/ad_sim.model")
    summary_sim = joblib.load("model/summary_sim.model")
    tags_sim = joblib.load("model/tags_sim.model")
    print("loading models successfully")
    # 下面这行读取的表的内容可能可以使用数据库代替
    movieid_to_index = pd.read_csv("model/movieid_to_index.csv")
    movieid_to_index = pd.Series(movieid_to_index.index, index=movieid_to_index['id'])
    # title_to_index = pd.read_csv("model/title_to_index.csv")
    # title_to_index = pd.Series(title_to_index.index, index=title_to_index['title'])
        
    
    # def title_to_movieid(title):
    #     print(AIrecommender.title_to_index[title])
    #     return AIrecommender.movieid_to_index.index[AIrecommender.title_to_index[title]]
    
    def recommend_tags_sim(index, start, num) :
        sim_scores = list(enumerate(AIrecommender.tags_sim[index]))
        sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
        return sim_scores[start:start+num]

    def recommend_ad_sim(index, start, num) :
        sim_scores = list(enumerate(AIrecommender.ad_sim[index]))
        sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
        return sim_scores[start:start+num]

    def recommend_summary_sim(index, start, num) :
        sim_scores = list(enumerate(AIrecommender.summary_sim[index]))
        sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
        return sim_scores[start:start+num]


def get_model_element(model, index, columns):
    sim_scores = list(enumerate(model[index]))
    return sim_scores[columns][1]


def recommend_by_all_model(movieId, start, num) : 
    index = AIrecommender.movieid_to_index[movieId]
    
    tags_sim_res = AIrecommender.recommend_tags_sim(index, start, num)
    
    ad_sim_res = AIrecommender.recommend_ad_sim(index, start, num)
    
    summary_sim_res = AIrecommender.recommend_summary_sim(index, start, num)
    
    movie_id_list = []
    for res in tags_sim_res:
        temp_dict = {}
        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]
        temp_dict['sim_score'] = (res[1] + get_model_element(AIrecommender.ad_sim, index, res[0]) + get_model_element(AIrecommender.summary_sim, index, res[0]))/3
        movie_id_list.append(temp_dict)
    for res in ad_sim_res:
        temp_dict = {}
        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]
        temp_dict['sim_score'] = (res[1] + get_model_element(AIrecommender.tags_sim, index, res[0]) + get_model_element(AIrecommender.summary_sim, index, res[0]))/3
        movie_id_list.append(temp_dict)
    for res in summary_sim_res:
        temp_dict = {}
        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]
        temp_dict['sim_score'] = (res[1] + get_model_element(AIrecommender.ad_sim, index, res[0]) + get_model_element(AIrecommender.tags_sim, index, res[0]))/3
        movie_id_list.append(temp_dict)

        
    res_list = sorted(movie_id_list, key=lambda x: x['sim_score'], reverse=True)

    return res_list


def recommend_by_tags_model(movieId, start, num) : 
    index = AIrecommender.movieid_to_index[movieId]
    tags_sim_res = AIrecommender.recommend_tags_sim(index, start, num)
    res_list = []
    for res in tags_sim_res:
        temp_dict = {}
        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]
        temp_dict['sim_score'] = res[1]
        res_list.append(temp_dict)
    return res_list
    



def recommend_by_ad_model(movieId, start, num) : 
    index = AIrecommender.movieid_to_index[movieId]
    ad_sim_res = AIrecommender.recommend_ad_sim(index, start, num)
    res_list = []
    for res in ad_sim_res:
        temp_dict = {}
        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]
        temp_dict['sim_score'] = res[1]
        res_list.append(temp_dict)
    return res_list



def recommend_by_summary_model(movieId, start, num) : 
    index = AIrecommender.movieid_to_index[movieId]
    summary_sim_res = AIrecommender.recommend_summary_sim(index, start, num)
    res_list = []
    for res in summary_sim_res:
        temp_dict = {}
        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]
        temp_dict['sim_score'] = res[1]
        res_list.append(temp_dict)
    return res_list



# a = AIrecommender.title_to_movieid("侏罗纪公园")
# print(a)
# recommend_by_all_model(movieId=a, start=1, num=20)



# recommend_by_tags_model(movieId=a, start=1, num=20)



# recommend_by_ad_model(movieId=a, start=1, num=20)



# recommend_by_summary_model(movieId=a, start=1, num=20)






